package com.aaa.ssm.controller;

import com.aaa.ssm.entity.Patient;
import com.aaa.ssm.entity.SchedulingInfo;
import com.aaa.ssm.entity.SysCareer;
import com.aaa.ssm.service.impl.SysCareerServiceImpl;
import com.aaa.ssm.util.Page;
import com.aaa.ssm.util.Result;
import com.aaa.ssm.vo.FileHandlerUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("syscareer")
public class SysCareerController extends BaseController{
    @GetMapping("login")
    @ResponseBody
    public Result login(Integer careerId, String password, HttpSession session){
        SysCareer sysCareer = sysCareerService.checkUser(careerId, password);
        if (sysCareer!=null){
            session.setAttribute("userInfo",sysCareer);
        }
        return success(sysCareer);
    }
    @Resource
    private SysCareerServiceImpl sysCareerService;
    @PostMapping("queryByPage")
    //@ResponseBody
    public Result queryByPage(@RequestBody Page<SysCareer> page){
        return success(sysCareerService.queryByPage(page));
    }

    @GetMapping("queryByParam")
    //@ResponseBody
    public Result queryByParam(@RequestBody SysCareer sysCareer) {
        List<SysCareer> sysCareerList = sysCareerService.queryByParam(sysCareer);
        return success(sysCareerList);
    }
    @GetMapping("queryById")
    //@ResponseBody
    public Result queryById(Integer careerId){
        return success(sysCareerService.queryById(careerId));
    }
    @PutMapping ("add")
    //@ResponseBody
    private Result add(@RequestParam("avatar") MultipartFile file, SysCareer sysCareer){
        if (file!=null){
            String uploadFilePath = FileHandlerUtil.uploadFile(file, "syscareer/images/");
            sysCareer.setAvatar(uploadFilePath);
        }
        sysCareerService.add(sysCareer);
        return success(sysCareerService.add(sysCareer));
    }

    @PutMapping("update")
    //@ResponseBody
    private Result update(@RequestBody SysCareer sysCareer){
        return success(sysCareerService.update(sysCareer));
    }

    @DeleteMapping("deleteById")
    //@ResponseBody
    private Result deleteById(Integer careerId){
        return success(sysCareerService.deleteById(careerId));
    }



    @GetMapping("/dateList")
    public Result<?> dateList(){
        Page <SysCareer> page =new Page();
        page.setPageNo(1);
        page.setPageSize(100);
        List<String> nowDateTo6LaterDayList  =new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (int i = 0; i <= 6; i++) {
            LocalDateTime localDateTime = now.plusDays(i);
            nowDateTo6LaterDayList.add(dateTimeFormatter.format(localDateTime));
        }
        System.out.println(nowDateTo6LaterDayList);

        PageInfo pageInfo = sysCareerService.queryByPage(page);
        List<SysCareer> list = pageInfo.getList();
        for (SysCareer sysCareer : list) {
            List<SchedulingInfo> schedulingInfoList =new ArrayList<>();
            for (String dateString : nowDateTo6LaterDayList) {
                schedulingInfoList.add(new SchedulingInfo(sysCareerService.querySchedulingInfo(sysCareer.getCareerId(),dateString),dateString));
            }
            sysCareer.setSchedulingInfo(schedulingInfoList);
            sysCareer.setDateStringList(nowDateTo6LaterDayList);
            
        }

        return Result.success(list);
    }



}
